<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category YbsArticleJob.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2021/8/19  14:57
 * +--------------------------------------------------------+
 */

namespace App\Jobs;


use App\Services\StockArticleService;
use App\Util\Container;
use App\Util\DingDing;
use Illuminate\Support\Facades\Log;

class YbsArticleJob extends Job
{

    //
    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 5;
    protected $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $insert_data = $this->data;

        try {
            // $StockArticleService = Container::getInstances(StockArticleService::class);
            // $StockArticleService->addData($insert_data);
            $StockArticleService = Container::run(StockArticleService::class, 'addData', $insert_data);

        } catch (\Exception $e) {
            // Log::info('ysb_article add error');
            Log::info($e->getMessage());
            return true;
        }

        //发送通知
        foreach ($insert_data as $value) {
            $summary = $value['summary'];
            if (empty($summary)) {
                $summary = $value['content'];
            }
            $title = $value['title'];


            // 发送邮件
            // $setting = sys_config();
            // try {
            //     if (!empty($setting['ybs_emial']) && $setting['ybs_emial'] == 1) {
            //         $email_arr = [$setting['site_email']];
            //         $send      = [
            //             'title'   => $title,
            //             'summary' => $summary,
            //         ];
            //         $view      = 'emails.article';
            //
            //         $email_data['title']     = $title;
            //         $email_data['summary']   = $summary;
            //         $email_data['email_arr'] = $email_arr;
            //         $email_data['send']      = $send;
            //         $email_data['view']      = $view;
            //         dispatch((new EmailsJob($email_data))->onQueue('default'));
            //     }
            // } catch (\Exception $e) {
            //     Log::info('ysb_article send email error');
            // }
            try {
                //发送钉钉
                // if (!empty($setting['ybs_dd']) && $setting['ybs_dd'] == 1) {
                // Log::info($title . PHP_EOL . $summary);
                $dingDing = new DingDing();
                $dingDing->textMsgSend($title . PHP_EOL . $summary, [], 2);
                // }
            } catch (\Exception $e) {
                Log::info('ysb_article send dingding error');
            }
            // try {
            //     //发送微信
            //     if (!empty($setting['ybs_weixin']) && $setting['ybs_weixin'] == 1) {
            //         StockArticleService::getInstance()->returnWxArticle($title);
            //     }
            // } catch (\Exception $e) {
            //     Log::info('ysb_article send weixin error');
            // }


        }


        return true;

    }

}
